home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-05-29 | 8.0 KB | 394 lines | [TEXT/CWIE] |
- // UCmdKeyBehavior.cp
- // Copyright © 1992-1995 Nick Nallick. All rights reserved.
- //
- //
- // Changes:
- //
- // • Respect the dim and enabled states of controls.
- // • Add MacApp 3.3 compatibility.
- // • Add MacApp 3.1 compatibility.
- //
-
- #ifndef __UCmdKeyBehavior__
- #include "UCmdKeyBehavior.h"
- #endif
-
- #pragma segment CmdKeyBehavior
-
-
- #if qMacAppVersion > 30
- MA_DEFINE_CLASS_M1(TCmdKeyBehavior, TBehavior);
- #endif
-
-
- #ifdef HANDLE_BASED_OBJECTS
-
- // Overrides TObject.
- // Initialize the fields.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::Initialize()
- {
- fKey = 0;
- fShiftedKey = 0;
- fCommandKeyOnly = FALSE;
- fBackupOwner = NULL;
- TBehavior::Initialize();
- }
-
- #elif qMacAppVersion <= 33
-
- // Initialize the fields.
- //
- TCmdKeyBehavior::TCmdKeyBehavior()
- {
- fKey = 0;
- fShiftedKey = 0;
- fCommandKeyOnly = FALSE;
- fBackupOwner = NULL;
- }
-
- #else
-
- // Initialize the fields.
- //
- TCmdKeyBehavior::TCmdKeyBehavior(IDType itsIdentifier, char cmdKey) :
- TBehavior(itsIdentifier),
- fKey(0),
- fShiftedKey(0),
- fCommandKeyOnly(FALSE),
- fBackupOwner(NULL)
- {
- SetKey(cmdKey);
- }
-
- // Added destructor -- MB-9805-29-1948
- TCmdKeyBehavior::~TCmdKeyBehavior()
- {
- }
-
- #endif
-
- #if qMacAppVersion <= 33
-
- // Initialize the behavior.
- //
- // cmdKey: the command key value
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::ICmdKeyBehavior(IDType itsIdentifier, char cmdKey)
- {
- IBehavior(itsIdentifier);
- SetKey(cmdKey);
- }
-
- #endif
- // Overrides TBehavior.
- // Handle the command key events.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::DoCommandKeyEvent(TToolboxEvent* event)
- {
- TBehavior::DoCommandKeyEvent(event);
-
- if (TToolboxEvent_GetCharacter(event) == fKey || TToolboxEvent_GetCharacter(event) == fShiftedKey)
- SendDefaultEvent();
- }
-
-
- // Overrides TBehavior.
- // Handle the key events.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::DoKeyEvent(TToolboxEvent* event)
- {
- TBehavior::DoKeyEvent(event);
-
- if (!fCommandKeyOnly && (TToolboxEvent_GetCharacter(event) == fKey || TToolboxEvent_GetCharacter(event) == fShiftedKey))
- SendDefaultEvent();
- }
-
-
- // Overrides TObject.
- // Return the standard signature.
- // This way if the user overrides the class, we'll still know what it is.
- //
- MACAPP_METHOD
- IDType TCmdKeyBehavior::GetStandardSignature() CONST
- {
- return kCmdKeySignature;
- }
-
-
- // Overrides TObject.
- // Read the fields.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::ReadFrom(TStream* stream)
- {
- TBehavior::ReadFrom(stream);
-
- char itsCmdKey = stream->ReadByte();
- Boolean itsCmdKeyState = stream->ReadBoolean();
- SetKey(itsCmdKey);
- SetCmdKeyOnly(itsCmdKeyState);
- }
-
-
- // Overrides TBehavior.
- // Maintain the backup owner field.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::SetOwner(TEventHandler* itsOwner)
- {
- TBehavior::SetOwner(itsOwner);
- fBackupOwner = itsOwner;
- }
-
-
- // Overrides TObject.
- // Write the fields.
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::WriteTo(TStream* stream) CONST
- {
- TBehavior::WriteTo(stream);
-
- stream->WriteByte(fKey);
- stream->WriteBoolean(fCommandKeyOnly);
- }
-
-
- // Get the key value.
- //
- // NO PARAMETERS
- //
- // RETURNS: the key value
- //
- MACAPP_METHOD
- char TCmdKeyBehavior::GetKey()
- {
- return fKey;
- }
-
-
- // Set the key value.
- //
- // newKey: the new key value
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::SetKey(char newKey)
- {
- if (newKey >= 'A' && newKey <= 'Z') // NOT case sensitive
- {
- fKey = newKey - 'A' + 'a';
- fShiftedKey = newKey;
- }
- else
- {
- fKey = newKey;
- fShiftedKey = newKey - 'a' + 'A';
- }
- }
-
-
- // Get the command key use state.
- //
- // NO PARAMETERS
- //
- // RETURNS: TRUE if the behavior only responds to command keys; FALSE otherwise
- //
- MACAPP_METHOD
- Boolean TCmdKeyBehavior::DoesCmdKeyOnly()
- {
- return fCommandKeyOnly;
- }
-
-
- // Set the command key use state.
- //
- // useCmdKeyOnly: if TRUE, only responds to command key events;
- // otherwise responds to all key events
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::SetCmdKeyOnly(Boolean useCmdKeyOnly)
- {
- fCommandKeyOnly = useCmdKeyOnly;
- }
-
-
- // Send the default event number to the owner.
- //
- // NO PARAMETERS
- //
- MACAPP_METHOD
- void TCmdKeyBehavior::SendDefaultEvent()
- {
- fOwner = fBackupOwner; // replace the owner so the default event propagates
- if (fOwner && !((TControl*) fOwner)->IsDimmed())
- {
- ((TControl*) fOwner)->Flash();
- fOwner->HandleEvent(((TControl*) fOwner)->GetEventNumber(), fOwner, NULL);
- }
- }
-
-
- ////////////////////////////// TBroadcastCmdKey //////////////////////////////
- //
- // A behavior which passes command key events to all the owners subviews.
- //
-
- class TBroadcastCmdKey : public TBehavior
- {
- #if qMacAppVersion > 30
- MA_DECLARE_CLASS;
- #endif
-
- public:
- VIRTUAL void DoCommandKeyEvent(TToolboxEvent* event);
- VIRTUAL void DoKeyEvent(TToolboxEvent* event);
- #if qMacAppVersion <= 33
- VIRTUAL void IBroadcastCmdKey(IDType itsIdentifier);
- #else
- TBroadcastCmdKey(IDType itsIdentifier = kNoIdentifier_AC);
- virtual ~ TBroadcastCmdKey();
- #endif
-
- private:
- static void BroadcastCmd(TView* view, TToolboxEvent* event, Boolean cmdKey);
- };
-
- #if qMacAppVersion > 30
- MA_DEFINE_CLASS_M1(TBroadcastCmdKey, TBehavior);
- #endif
-
-
- // Overrides TBehavior.
- // Handle the command key events.
- //
- MACAPP_METHOD
- void TBroadcastCmdKey::DoCommandKeyEvent(TToolboxEvent* event)
- {
- TBehavior::DoCommandKeyEvent(event);
-
- if (!event->IsAutoKeyEvent())
- {
- CSubViewIterator iter((TView*) fOwner);
- for (TView* subview = iter.FirstSubView(); iter.Current(); subview = iter.NextSubView())
- BroadcastCmd(subview, event, TRUE);
- }
- }
-
-
- // Overrides TBehavior.
- // Handle the key events.
- //
- MACAPP_METHOD
- void TBroadcastCmdKey::DoKeyEvent(TToolboxEvent* event)
- {
- TBehavior::DoKeyEvent(event);
-
- if (!event->IsAutoKeyEvent())
- {
- CSubViewIterator iter((TView*) fOwner);
- for (TView* subview = iter.FirstSubView(); iter.Current(); subview = iter.NextSubView())
- BroadcastCmd(subview, event, FALSE);
- }
- }
-
- #if qMacAppVersion <= 33
-
- // Initialize the behavior.
- //
- // cmdKey: the command key value
- //
- MACAPP_METHOD
- void TBroadcastCmdKey::IBroadcastCmdKey(IDType itsIdentifier)
- {
- IBehavior(itsIdentifier);
- }
-
- #else
-
- // Initialize the behavior.
- //
- // cmdKey: the command key value
- //
- TBroadcastCmdKey::TBroadcastCmdKey(IDType itsIdentifier) :
- TBehavior(itsIdentifier)
- {
- }
-
- // Added destructor -- MB-9805-29-1950
- TBroadcastCmdKey::~TBroadcastCmdKey()
- {
- }
-
- #endif
-
-
- // Recursively send the command to a view's behaviors and its subview's behaviors.
- //
- // view: the top level view
- // event: the key event
- // cmdKey: if TRUE, this is a command key event; otherwise it's a plain key event
- //
- void TBroadcastCmdKey::BroadcastCmd(TView* view, TToolboxEvent* event, Boolean cmdKey)
- {
- // Before sending the event to the behavior chain we disconnect it from its
- // owner to prevent the event from coming back up the target chain to the
- // behavior and causing an infinite loop. This is accomplished by temporarily
- // setting the owner of the last behavior in the chain to NULL.
- // TCmdKeyBehavior saves a backup copy of its owner so it still knows
- // where to send the message.
-
- if (view->IsEnabled())
- {
- TBehavior* firstBehavior = view->GetFirstEnabledBehavior();
- if (firstBehavior)
- {
- // first find the last behavior in the chain
- TBehavior* lastBehavior = firstBehavior;
- TBehavior* nextBehavior = firstBehavior->GetNextEnabledBehavior();
- while (nextBehavior)
- {
- lastBehavior = nextBehavior;
- nextBehavior = nextBehavior->GetNextEnabledBehavior();
- }
-
- // next, save the owner, set it to NULL, and send the event
- TEventHandler* owner = TBehavior_GetOwner(lastBehavior);
- TBehavior_SetOwner(lastBehavior, NULL);
- if (cmdKey)
- firstBehavior->DoCommandKeyEvent(event);
- else
- firstBehavior->DoKeyEvent(event);
-
- // finally, reset the owner
- TBehavior_SetOwner(lastBehavior, owner);
- }
- }
-
- // send it to the subviews
- CSubViewIterator iter(view);
- for (TView* subview = iter.FirstSubView(); iter.ITERCURRENT(); subview = iter.NextSubView())
- BroadcastCmd(subview, event, cmdKey);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
-
- #pragma segment AInit
-
- void RegisterTCmdKeyBehavior(); // function prototype
-
- void RegisterTCmdKeyBehavior()
- {
- #if qMacAppVersion == 30
- new TCmdKeyBehavior;
- new TBroadcastCmdKey;
- #else
- MA_REGISTER_SIGNATURE(TCmdKeyBehavior, kCmdKeySignature);
- MA_REGISTER_CLASS(TBroadcastCmdKey);
- #endif
- }